/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.VisualStudio.Modeling;

using DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid.Data;
using DslEditorPowerToy.Languages;

namespace DslEditorPowerToy.Controls.Editors.Special
{
    public partial class DomainModelTreeGridEditor : DslEditorControlBase
    {
        #region Constructors
        public DomainModelTreeGridEditor()
        {
            InitializeComponent();
        }
        #endregion

        #region Protected Methods
        public override void OnRootElementChanged(ModelElement rootElement)
        {
            base.OnRootElementChanged(rootElement);

            //Set selected object
            SetSelectedObject(rootElement);
        }
        /// <summary>
        /// Handles selection change
        /// </summary>
        /// <param name="selectionProvider"></param>
        /// <param name="source"></param>
        protected override void OnNotifySelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
            base.OnNotifySelectionChanged(selectionProvider, source);

            //Only handle diagram selection change
            if (SelectionChangeSource.Diagram == source)
            {            
                //Selects the specified object
                if (null != selectionProvider)
                {
                    //Get represented ME
                    SetSelectedObject(ModelHelper.GetRepresentedElement(selectionProvider.PrimarySelection));
                }
            }
        }
        protected override ICollection SelectableComponents
        {
            get
            {
                object currentSelection = this.treegrid.CurrentSelection;
                if (null == currentSelection)
                    return null;
                else
                {
                    ArrayList list = new ArrayList();
                    list.Add(UnwrapProxy(currentSelection));
                    return list;
                }
            }
        }

        protected override bool IsComponentSelected(object component)
        {
            return (UnwrapProxy(treegrid.DataSource) == component);
        }
        #endregion

        #region Control Handlers
        void treegrid_SelectionChanged(object sender, DslEditorPowerToy.Controls.VirtualTreeGrid.SelectionChangedEventArgs e)
        {
            OnSelectionChanged();
        }

        #endregion

        #region Private Methods
        /// <summary>
        /// Sets selected object to treegrid
        /// </summary>
        /// <param name="provider"></param>
        private void SetSelectedObject(object selectedObject)
        {
            //Set selected object
            this.treegrid.DataSource = WrapInProxy(selectedObject);
        }
        /// <summary>
        /// Wraps ME in proxy
        /// </summary>
        /// <returns></returns>
        private IDomainProxy WrapInProxy(object selectedObject)
        {
            if ((null == selectedObject)
                || (!(selectedObject is ModelElement)))
                return null;
            
            //Derefence element to a porxy
            ModelElement selectedElement = selectedObject as ModelElement;
            if (selectedElement == this.RootElement)
                return DomainElementProxy.GetInstance(selectedElement);
            else if (selectedElement is ElementLink)
            {
                if (((ElementLink)selectedElement).GetDomainRelationship().IsEmbedding)
                    return DomainElementProxy.GetInstance(selectedElement as ElementLink);
                else
                    return null;
            }
            else
                return DomainElementProxy.GetInstance(selectedElement);
        }
        /// <summary>
        /// Returns unwrapped ME
        /// </summary>
        private object UnwrapProxy(object selectedObject)
        {
            if (null == selectedObject)
                return null;

            //Upack a proxy
            if (selectedObject is IDomainProxy)
                return (selectedObject as IDomainProxy).Element;
            else
                return selectedObject;
        }
        #endregion
    }
}
